﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Entity;

namespace Qu.SeniorProject.Model
{
    public class UserRepository
    {
        public static UserInfo GetUserInfo(string loginName)
        {
            var spDB = new SeniorProjectEntities();
            var userInfo = spDB.Database.SqlQuery<UserInfo>("GetUserInfo @loginName", new SqlParameter("loginName", loginName.Trim())).FirstOrDefault();

            if (loginName.Equals("abetteam", StringComparison.OrdinalIgnoreCase))
            {
                userInfo = new UserInfo();
                userInfo.FullName = "ABET Team";
                userInfo.IsAdmin = false;
                userInfo.UserType = "ABET";
                userInfo.LoginName = "abetteam";
                userInfo.UserID = "abetteam";
            }

            return userInfo;
        }

        public static string[] GetStudents(string query)
        {
            var spDB = new SeniorProjectEntities();
            var students = (from u in spDB.Students
                where u.StudentId.StartsWith(query)
                orderby u.StudentId // optional but it'll look nicer
                select u.StudentId + " - " + u.FirstName + " " + u.LastName).Distinct().ToArray();

            return students;
        }

        public static List<Faculty> GetAll()
        {
            var spDB = new SeniorProjectEntities();
            return spDB.Faculties.ToList();
        }

        public static string GetFacultyName(int facultyID)
        {
            var spDB = new SeniorProjectEntities();
            return spDB.Faculties.Where(f => f.FacultyId == facultyID).FirstOrDefault().Name;
        }
    }
}
